//
//  AppDelegate.swift
//  Weibo
//
//  Created by 冯童童 on 16/1/11.
//  Copyright © 2016年 冯童童. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        setThemeColor()
        registerNotification()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.greenColor()
        
         window?.rootViewController = defaultRootViewController()
//        let compose = PictureSelectorViewController()
//        let nav = UINavigationController(rootViewController: compose)
//
//       window?.rootViewController = nav
        
      window?.makeKeyAndVisible()

        
        return true
    }

    private func setThemeColor(){
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
   
    
    private func registerNotification(){
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "chooseRootVC:", name: WBSwitchRootVC, object: nil)
        
    }
    
    @objc private func chooseRootVC(n : NSNotification) {
        
//        print(n)
//        if n.object  != nil {
//            window?.rootViewController = WelcomeViewController()
//        } else{
//            window?.rootViewController = MainViewController()
//        }
        window?.rootViewController = n.object == nil ? MainViewController() : WelcomeViewController()
        
    }
    
    deinit {
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    private func defaultRootViewController() -> UIViewController {
        
//        if UserAccountViewModel().userLogin {
//            
//            return WelcomeViewController()
//        }
//    
//        return MainViewController()
        return UserAccountViewModel().userLogin ? WelcomeViewController() : MainViewController()
    }
    
    
}

